#!C:\WebServer\Python2.4\python.exe
print "Content-Type: text/html\n\n"
import cgi
import SyntaxParser
import SQLGenerator
import answer
import condition
import patientCondition

form = cgi.FieldStorage()
toc = str(form.getvalue("toc"))
db = str(form.getvalue("db"))
answer = str(form.getvalue("answer"))
patient_conditions = str(form.getvalue("patient_conditions"))
conditions = str(form.getvalue("conditions"))
time_entity = str(form.getvalue("time_entity"))
time_value = str(form.getvalue("time_value"))
ward = str(form.getvalue("ward"))

answer_array = []

path = '.\doc\interface\dictionary.txt'
dictionary = SyntaxParser.buildHash(path)

path = '.\doc\interface\patient_attributes.txt'
patient_attributes = SyntaxParser.buildHash(path)

ref_fns = ["all values", "any value"]
stat_fns = ["mean", "sd", "median"]
answer_array = SyntaxParser.getAnswers(answer, ref_fns, stat_fns, dictionary)

comparands = [">",">=","<","<=","=","<>"]
types = ["and", "or"]
patient_conditions_ = SyntaxParser.getConditions(patient_conditions, comparands, types, patient_attributes, 'T')
conditions_ = SyntaxParser.getConditions(conditions, comparands, types, dictionary)

hours = SyntaxParser.getHours(time_entity, time_value)

print "<html>"
print "<head>"
print "<title>CDAL GUI V1.0</title>"
print "<style type='text/css'>"
print "body {font-family: Verdana, Tahoma, Sans-serif;font-size: 10pt;background: #B0E2FF;}"
print "table {text-align: center;font-size: 10pt;}"
print "</style>"
print "</head>"

print "<body>"
print "<div id='cdal_test'>"
print "<table border='1'>"
print "<tr style='background:#FFEC8B;'><th>Attr</th><th colspan='4'>Value</th></tr>"
print "<tr><td>TOC</td><td colspan='4'>" + toc + "</td></tr>"
print "<tr><td>DB</td><td colspan='4'>" + db + "</td></tr>"
print "<tr><td>Answers</td><td style='background:#F0FFF0;'>Function</td><td style='background:#F0FFF0;'>Fn Type</td><td colspan='2' style='background:#F0FFF0;'>Variable</td></tr>"
for answer in answer_array:
    ref_fns = answer.getRef_fn()
    variable = answer.getVariable()
    if ref_fns is None:
        print "<tr><td></td><td>-</td><td>Ref</td><td colspan='2'>" + variable + "</td></tr>"
    else:
        for ref_fn in ref_fns:
            print "<tr><td></td><td>" + ref_fn + "</td><td>Ref</td><td colspan='2'>" + variable + "</td></tr>"
        
    stat_fns = answer.getStat_fn()
    if stat_fns is None:
        print "<tr><td></td><td>-</td><td>Stat</td><td colspan='2'>" + variable + "</td></tr>"
    else:
        for stat_fn in stat_fns:
            print "<tr><td></td><td>" + stat_fn + "</td><td>Stat</td><td colspan='2'>" + variable + "</td></tr>"
            
print "<tr><td>Patient Conditions</td><td style='background:#F0FFF0;'>Type</td><td style='background:#F0FFF0;'>Attribute</td><td style='background:#F0FFF0;'>Comparand</td><td style='background:#F0FFF0;'>Value</td></tr>"
patient_and_conditions = patient_conditions_.getAnd_conditions()
patient_or_conditions = patient_conditions_.getOr_conditions()
for patient_and_condition in patient_and_conditions:
    type_ = patient_and_condition.getType()
    attribute = patient_and_condition.getAtt()
    comparand = patient_and_condition.getComp()
    value = patient_and_condition.getVal()
    print "<tr><td></td><td>" + type_ + "</td><td>" + attribute + "</td><td>" + comparand + "</td><td>" + value + "</td></tr>"
    
for patient_or_condition in patient_or_conditions:
    type_ = patient_or_condition.getCondition1().getType()
    attribute = patient_or_condition.getCondition1().getAtt()
    comparand = patient_or_condition.getCondition1().getComp()
    value = patient_or_condition.getCondition1().getVal()
    print "<tr><td></td><td>" + type_ + "</td><td>" + attribute + "</td><td>" + comparand + "</td><td>" + value + "</td></tr>"
    type_ = patient_or_condition.getCondition2().getType()
    attribute = patient_or_condition.getCondition2().getAtt()
    comparand = patient_or_condition.getCondition2().getComp()
    value = patient_or_condition.getCondition2().getVal()
    print "<tr><td></td><td>" + type_ + "</td><td>" + attribute + "</td><td>" + comparand + "</td><td>" + value + "</td></tr>"

print "<tr><td>Conditions</td><td style='background:#F0FFF0;'>Type</td><td style='background:#F0FFF0;'>Attribute</td><td style='background:#F0FFF0;'>Comparand</td><td style='background:#F0FFF0;'>Value</td></tr>"
and_conditions = conditions_.getAnd_conditions()
or_conditions = conditions_.getOr_conditions()
for and_condition in and_conditions:
    type_ = and_condition.getType()
    attribute = and_condition.getAtt()
    comparand = and_condition.getComp()
    value = and_condition.getVal()
    print "<tr><td></td><td>" + type_ + "</td><td>" + attribute + "</td><td>" + comparand + "</td><td>" + value + "</td></tr>"

for or_condition in or_conditions:
    type_ = or_condition.getCondition1().getType()
    attribute = or_condition.getCondition1().getAtt()
    comparand = or_condition.getCondition1().getComp()
    value = or_condition.getCondition1().getVal()
    print "<tr><td></td><td>" + type_ + "</td><td>" + attribute + "</td><td>" + comparand + "</td><td>" + value + "</td></tr>"
    type_ = or_condition.getCondition2().getType()
    attribute = or_condition.getCondition2().getAtt()
    comparand = or_condition.getCondition2().getComp()
    value = or_condition.getCondition2().getVal()
    print "<tr><td></td><td>" + type_ + "</td><td>" + attribute + "</td><td>" + comparand + "</td><td>" + value + "</td></tr>"
    
print "<tr><td>Hours</td><td colspan='4'>" + str(hours) + "</td></tr>"
print "<tr><td>Ward</td><td colspan='4'>" + ward + "</td></tr>"
print "</table>"
print "</div>"
print "</body>"

print "</html>"
